package com.example.myapplication.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.myapplication.BaseOnclickListener;
import com.example.myapplication.R;
import com.example.myapplication.entity.SearchResult;

import java.util.List;

public class ResultSongListAdapter extends RecyclerView.Adapter<ResultSongListAdapter.ResultSongListViewHolder>{
    private List<SearchResult.PlaylistsBean> playlists;
    private Context context;
    private BaseOnclickListener onclickListener;

    public ResultSongListAdapter(Context context) {
        this.context = context;
    }

    public void setData(List<SearchResult.PlaylistsBean> playlists){
        this.playlists = playlists;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ResultSongListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ResultSongListViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_result_song_list, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ResultSongListViewHolder holder, int position) {
        SearchResult.PlaylistsBean playlistsBean = playlists.get(position);
        SearchResult.PlaylistsBean.CreatorBean creatorBean = playlistsBean.getCreator();

        Glide.with(context)
                .load(playlistsBean.getCoverImgUrl())
                .apply(RequestOptions.bitmapTransform(new RoundedCorners(40)))
                .into(holder.img);

        holder.tvCreator.setText(creatorBean.getNickname());
        holder.tvSongListName.setText(playlistsBean.getName());
        holder.tvSongListCount.setText(playlistsBean.getTrackCount());

        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onclickListener != null){
                    onclickListener.click(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return playlists == null ? 0 : playlists.size();
    }


    class ResultSongListViewHolder extends RecyclerView.ViewHolder{
        ImageView img;
        TextView tvSongListName;
        TextView tvSongListCount;
        TextView tvCreator;
        View view;

        public ResultSongListViewHolder(@NonNull View itemView) {
            super(itemView);

            img = itemView.findViewById(R.id.song_list_cover);
            tvSongListName = itemView.findViewById(R.id.tv_song_list_name);
            tvSongListCount = itemView.findViewById(R.id.tv_song_list_count);
            tvCreator = itemView.findViewById(R.id.tv_creator);
            view = itemView.findViewById(R.id.view_layout);
        }
    }

    public void setBaseOnclickListener(BaseOnclickListener onclickListener){
        this.onclickListener = onclickListener;
    }
}
